﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DATA_operate_;

public partial class design_page_student_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_myinfo = new DATA_operate();   //创建数据库操作类对象

   


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["stuid"];
            string stuid = cookie.Value.ToString();
            /*
            string sqlsqlStr = @"SELECT _main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_teacher._Tname,REPLACE( c._Hid,c._Hid,'未完成') AS 是否完成
                         FROM _main_homework,_teacher,(SELECT a._Hid FROM( SELECT DISTINCT _class_homework._Hid FROM _class_homework,_student WHERE _student._SCid = _class_homework._Cid) AS a
                       EXCEPT
                         SELECT b._Hid FROM ( SELECT _student_homework._Hid FROM _student_homework,_student WHERE _student._Sid = '" + stuid + @"' AND _student_homework._Sid = _student._Sid ) AS b ) AS c 
                          WHERE _main_homework._mHTid = _teacher._Tid 
                         AND _main_homework._mHid = c._Hid";
             * */
            string sql_not_finish = @"select _main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_teacher._Tname from _student_homework,_main_homework,_teacher where _mHid in 
                           (select _Hid from  _student_homework where _Sid='" + stuid + @"') and _Sid='" + stuid + @" ' and _Hfinishcontent is   null and _Hid=_mHid and _Tid in 
                        (select _mHTid from _main_homework,_teacher where _mHid in (select _Hid from  _student_homework where _Sid='" + stuid + @" ') and _Hid=_mHid and _Hfinishcontent is   null and 
                              _Sid='" + stuid + " ')";
            /*
            SqlConnection con = data_operate_in_myinfo.databaseopen();
            SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView2.DataSource = ds;
            this.GridView2.DataBind();
             * */
            data_operate_in_myinfo.Data_bind(sql_not_finish, GridView2, "_mHid");


            string sql_finishied = @"select  _main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_student_homework._Hfinishtime,_Tname,_Hscore from _student_homework,_main_homework,_teacher where _mHid in 
                           (select _Hid from  _student_homework where _Sid='" + stuid + @"') and _Sid='" + stuid + @" ' and _Hfinishcontent is not  null and _Hid=_mHid and _Tid in 
                        (select _mHTid from _main_homework,_teacher where _mHid in (select _Hid from  _student_homework where _Sid='" + stuid + @" ') and _Hid=_mHid and _Hfinishcontent is not  null and 
                              _Sid='" + stuid + " ')";
            data_operate_in_myinfo.Data_bind(sql_finishied, GridView1, "_mHid");

            detail_homework.Style.Add("display", "none");   //隐藏详细作业信息的DIV控件


            
        }
    }
    protected void GVSecondType_RowCommand(object sender, GridViewCommandEventArgs e)   //已完成的
    { /*选定得到指定字段值并根据他进行sql 查询获得指定信息*/
        

    }
    /*grideview 里 自定义的一个单击事件<asp:LinkButton ID="LinkButton2" CommandName="selectunfinish"  runat="server" CommandArgument ='<%# Eval("作业题号") %>' >选择</asp:LinkButton>  */
    protected void GVSecondType2_RowCommand(object sender, GridViewCommandEventArgs e)   //未完成
    { /*选定得到指定字段值并根据他进行sql 查询获得指定信息*/
        DATA_operate chose = new DATA_operate();

        this.TextBox4.Text = "";
        this.TextBox1.Text = "";
        this.TextBox2.Text = "";
        this.TextBox3.Text = "";


        main_homework.Style.Add("display", "none");  //隐藏显示所有作业粗略信息的数据表
        detail_homework.Style.Add("display", "block");   //显示改作业的详细信息
        this.Label1.Visible = false;
        this.TextBox3.Visible = false;
        this.Button4.Visible = true;
        this.Button1.Visible = true;

        


      

        string cmdName = e.CommandName;
        if (cmdName == "selectfinish1")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView2.Rows[index];
            string homeworkid = ((Label)gvr.FindControl("dLabel_homeworkid")).Text;
            HttpCookie cookie2 = Request.Cookies["stuid"];
            string stuid = cookie2.Value.ToString();

            now_do_homeworkid.Text = homeworkid;   //选择进入学生未完成的作业后记录下这个作业ID
            // string str_classname = this.GridView2.Rows[index].Cells[1].Text;
           
            /* string[] estr = e.CommandArgument.ToString().Split(',');
             string homeworkid = Convert.ToString(estr[0]);*/
            chose.databaseopen();
            this.TextBox4.Text = chose.dataread("_main_homework", "_mHname", "_mHid", homeworkid);
            this.TextBox2.Text = chose.dataread("_main_homework", "_mHContent", "_mHid", homeworkid);
           // this.TextBox2.Text = chose.dataread("_student_homework", "_Hfinishcontent", "_Hid = '" + homeworkid + "' and _Sid", stuid);
            chose.databaseclose();


        }
    }

    protected void Button4_Click(object sender, System.EventArgs e)   //返回操作
    {


        main_homework.Style.Add("display", "block");  //显示所有作业粗略信息的数据表
        detail_homework.Style.Add("display", "none");   //隐藏改作业的详细信息
      
        this.Label1.Visible = true;
        this.TextBox3.Visible = true;

        this.Button4.Visible=false;
        this.Button1.Visible =false;

        Response.Redirect(Request.RawUrl);
    }

    protected void Button1_Click(object sender, System.EventArgs e)    //作业提交
    {
        string homeworkid = now_do_homeworkid.Text;   // 当前要提交的作业ID
        HttpCookie cookie2 = Request.Cookies["stuid"];
        string stuid = cookie2.Value.ToString();   //学生ID
        string finishcontent=TextBox1.Text;  //获得学生答案
        string uptime = DateTime.Now.ToLocalTime().ToString(); 
        if (finishcontent=="")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('提交的答案不能为空');", true);

        }
        else
        {
            this.Button1.Attributes.Add("onclick", "return confirm('确定要提交么?');");
            if (data_operate_in_myinfo.Data_update("_student_homework", "_Hfinishtime", uptime, "_Sid", stuid, "_Hid", homeworkid))
            {
                if (data_operate_in_myinfo.Data_update("_student_homework", "_Hfinishcontent", finishcontent, "_Sid", stuid, "_Hid", homeworkid))
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('提交成功,再次提交即可修改');", true);
            }
            else
                 Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('::>_<:: 提交失败了。可尝试再次提交。或联系我们');", true);                  
        }

   }
    protected void GVSecondType1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DATA_operate chose = new DATA_operate();


        main_homework.Style.Add("display", "none");  //隐藏显示所有作业粗略信息的数据表
        detail_homework.Style.Add("display", "block");   //显示改作业的详细信息
        // 

        this.Button4.Visible = true;


        string cmdName = e.CommandName;
        if (cmdName == "selectfinish")
        {

            HttpCookie cookie1 = Request.Cookies["stuid"];
            string stuid = cookie1.Value.ToString();

            
            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow gvr = GridView1.Rows[index];
            string homeworkid = ((Label)gvr.FindControl("dLabel_homeworkid1")).Text;  
              
    
            chose.databaseopen();
            this.TextBox4.Text = chose.dataread("_main_homework", "_mHname", "_mHid", homeworkid);
            this.TextBox2.Text = chose.dataread("_main_homework", "_mHContent", "_mHid", homeworkid);   //作业内容
            this.TextBox1.Text = chose.dataread("_student_homework", "_Hfinishcontent", "_Hid = '" + homeworkid + "' and _Sid", stuid);//我的答案
            this.TextBox3.Text = chose.dataread("_main_homework", "_mTrueanswer", "_mHid", homeworkid);
            chose.databaseclose();

        }
    }
    protected void gridview_pageindexchanging(object sender, GridViewPageEventArgs e)
    {
        HttpCookie cookie = Request.Cookies["stuid"];
        string stuid = cookie.Value.ToString();
        string sql_finishied = @"select  _main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_student_homework._Hfinishtime,_Tname,_Hscore from _student_homework,_main_homework,_teacher where _mHid in 
                           (select _Hid from  _student_homework where _Sid='" + stuid + @"') and _Sid='" + stuid + @" ' and _Hfinishcontent is not  null and _Hid=_mHid and _Tid in 
                        (select _mHTid from _main_homework,_teacher where _mHid in (select _Hid from  _student_homework where _Sid='" + stuid + @" ') and _Hid=_mHid and _Hfinishcontent is not  null and 
                              _Sid='" + stuid + " ')";
        data_operate_in_myinfo.Data_bind(sql_finishied, GridView1, "_mHid");
    }
    protected void gridview2_pageindexchanging(object sender, GridViewPageEventArgs e)
    {
        HttpCookie cookie = Request.Cookies["stuid"];
        string stuid = cookie.Value.ToString();
     
        string sql_not_finish = @"select _main_homework._mHid,_main_homework._mHname,_main_homework._mHupdatetime,_teacher._Tname from _student_homework,_main_homework,_teacher where _mHid in 
                           (select _Hid from  _student_homework where _Sid='" + stuid + @"') and _Sid='" + stuid + @" ' and _Hfinishcontent is   null and _Hid=_mHid and _Tid in 
                        (select _mHTid from _main_homework,_teacher where _mHid in (select _Hid from  _student_homework where _Sid='" + stuid + @" ') and _Hid=_mHid and _Hfinishcontent is   null and 
                              _Sid='" + stuid + " ')";
    
        data_operate_in_myinfo.Data_bind(sql_not_finish, GridView2, "_mHid");


    }
}